define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $stateParams, Employeemanager_add, GoDialog, GoPage, $ionicPopup, $ionicModal) {

		//接收参数
		$scope.input = new Object();
		$scope.input.id = $stateParams.id;
		$scope.input.phone = "";
		$scope.input.password = "";
		$scope.input.repassword = "";
		$scope.input.part_id = "";
		$scope.input.part_name = "请选择";
		$scope.input.name = "";
		$scope.input.mail = "";

		$scope.title = "添加店员";
		if($scope.input.id) {
			$scope.title = "修改店员";
		}
		//初始化数据
		$scope.data_init = function() {
			if($scope.input.id) { //根据id查找
				Employeemanager_add.assistente_detail($scope.input.id, function(data) {
					if(data.result != null) {
						$scope.input.phone = data.result.phone;
						$scope.input.part_id = data.result.part_id;
						$scope.input.name = data.result.name;
						$scope.input.mail = data.result.mail;
					}
				})
			}

			Employeemanager_add.part_list(function(data) {
				$scope.part_role = data.role;
				$scope.part_lists = data.role;
			});
		}
		$scope.data_init();

		//输入监听
		$scope.$watch('input.phone', function(newValue, oldValue) {
			$scope.textLength = newValue.length;
			if($scope.textLength > 11) {
				//$scope.input.phone=oldValue
				$scope.input.phone = newValue.substring(0, 11);
			}
		})
		$scope.$watch('input.password', function(newValue, oldValue) {
			$scope.textLength = (newValue + "").length;
			if($scope.textLength > 20) {
				// $scope.input.password = oldValue;
				$scope.input.password = newValue.substring(0, 20);
			}
		})
		$scope.$watch('input.repassword', function(newValue, oldValue) {
			$scope.textLength = (newValue + "").length;
			if($scope.textLength > 20) {
				// $scope.input.repassword = oldValue;
				$scope.input.repassword = newValue.substring(0, 20);
			}
		})

		//手机验证
		$scope.flag=false;//记录手机号码是否可用
		$scope.msg="请输入正确手机号码";//记录错误信息
		$scope.namecheck = function() {			
			var phoneflag = /[0-9]/i;
			if($scope.input.phone.length != 11 || !phoneflag.test($scope.input.phone)) {
				GoDialog.toast("请输入正确手机号码");
				return;
			}			
			Employeemanager_add.namecheck($scope.input.phone, function(data) {
				if(!data.success) {
					GoDialog.toast(data.msg);
					$scope.flag=false;
					$scope.msg=data.msg;
				}else{
					$scope.flag=true;
				}
			})
		}
		//
		$scope.showTips = function() {
			$ionicPopup.show({
				template: $scope.msg,
				title: "提示",
				//				scope: $scope, //绑定数据，即要想在cart_notice.html里使用$scope.msg，必须写这行
				buttons: [{
						text: "取消",
						type: "button-assertive",
						onTap: function() {

						}
					},
					{
						text: "确定",
						type: "button-positive",
						onTap: function() { // 当点击保存按钮时，执行该函数
							if(type == 3) {
								$scope.change_comment(comment_id);
							} else { //1和2删除评价
								$scope.delete_comment(comment_id);
							}
						}
					}
				]
			})
			GoDialog.toast("是否用此用户名注册企业用户！");
		}

		//控制数字
		$scope.clearNoNum = function(obj, attr) {
			//先把非数字的都替换掉，除了数字
			obj[attr] = obj[attr].replace(/[^\d]/g, "");
		}

		//新增
		$scope.add = function() {			
			if(!$scope.flag){
				GoDialog.toast($scope.msg);
				return;
			}
			if($scope.input.password.toString().length < 6 || $scope.input.password.toString().length > 20) {
				GoDialog.toast("请输入6-20位密码,可包含数字，英文和符号");
				return;
			}
			if($scope.input.mail) {
				var emailsRegexp = /^([a-z0-9!#$%&'*+\/=?^_`{|}~.-]+@[a-z0-9-]+(\.[a-z0-9-]+)*[;；]?)+$/i;
				if(!emailsRegexp.test($scope.input.mail)) {
					GoDialog.toast("请输入正确邮箱！格式：xxx@xx.com");
					return;
				}
			}
			if(!$scope.input.phone) {
				GoDialog.toast("请输入手机号！");
			} else if(!$scope.input.password) {
				GoDialog.toast("请输入密码！");
			} else if(!$scope.input.repassword) {
				GoDialog.toast("请再次输入密码！");
			} else if($scope.input.part_id == "") {
				GoDialog.toast("请选择角色！");
			} else if($scope.input.password != $scope.input.repassword) {
				GoDialog.toast("您两次输入的密码不相同！");
			} else {
				$ionicPopup.show({
					template: "是否用此用户名注册企业用户？",
					title: "提示",
					buttons: [{
						text: "取消",
						type: "button-stable",
						onTap: function() {}
					}, {
						text: "确定",
						type: "button-assertive",
						onTap: function() {
							Employeemanager_add.assistente_edit($scope.input.id, $scope.input.phone, $scope.input.password, $scope.input.mail, $scope.input.name, $scope.input.part_id, function(data) {
								if(data.success) {
									GoDialog.toast("提交成功！");
									GoPage.go_states("employeemanager");
								} else {
									GoDialog.toast(data.msg);
								}
							});
						}
					}]
				})
			}
		}

		//提交表单
		$scope.save = function() {
			if($scope.input.id) { //修改
				if(!$scope.input.part_id) {
					GoDialog.toast("请选择角色！");
				} else {
					Employeemanager_add.modify($scope.input.id, $scope.input.part_id, function(data) {
						if(data.success) {
							GoDialog.toast("修改成功！");
							GoPage.go_states("employeemanager");
						} else {
							GoDialog.toast(data.msg);
						}
					});
				}
			} else { //新增
				$scope.add();
			}
		}
		//清空input
		$scope.clearinput = function(type) {
			if(type == 1) {
				$scope.input.phone = "";
			} else if(type == 2) {
				$scope.input.password = "";
			} else {
				$scope.input.repassword = "";
			}
		}
		//给商品添加标签
		$ionicModal.fromTemplateUrl("templates/modal/rebuildRole.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal = modal;
		});
		//打开
		$scope.openModal_tag = function() {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				if($scope.part_lists[i].id == $scope.input.part_id) {
					$scope.part_lists[i].isSelected = true;
				} else {
					$scope.part_lists[i].isSelected = false;
				}
			}
			$scope.modal.show();
		}
		//隐藏
		$scope.closeModal_tag = function() {
			$scope.modal.hide();
		}
		//暂时
		$scope.selectTagId = function(index) {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				$scope.part_lists[i].isSelected = false;
			}
			$scope.part_lists[index].isSelected = true;
		}
		//修改店员角色
		$scope.updatarole = function() {
			for(var i = 0; i < $scope.part_lists.length; i++) {
				if($scope.part_lists[i].isSelected) {
					$scope.input.part_name = $scope.part_lists[i].name;
					$scope.input.part_id = $scope.part_lists[i].id;
					$scope.closeModal_tag();
				}
			}

		}

	}

	controller.$inject = ['$scope', '$stateParams', 'Employeemanager_add', 'GoDialog', 'GoPage', '$ionicPopup', '$ionicModal'];

	return app.registerController("employeemanager_addctrl", controller);
});